<template>
  <div>
    <el-card style="margin: 10px 0">
      <div>
        <!-- getcategoryid获取组件3个id，show控制组件是否可选 -->
        <CategorySelect @getcategoryId="getcategoryid" :show="scene != 0" />
      </div>
    </el-card>
    <!-- 展示部分有三部分内容切换显示 -->
    <el-card>
      <!-- 展示spu -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-bottom: 10px"
          :disabled="!id.category3Id"
          @click="addSpu()"
          >添加SPU</el-button
        >
        <el-table style="width: 100%" :data="records" border>
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column
            prop="spuName"
            label="spu名称"
            width="width"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="description"
            label="sup描述"
            width="width"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="prop"
            label="操作"
            width="width"
            align="center"
          >
            <template slot-scope="{ row }">
              <hint-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                title="添加sku"
                @click="addSku(row)"
              />
              <hint-button
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="updateSpu(row)"
              />
              <hint-button
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看当前spu所有的sku列表"
                @click="showAll(row)"
              />
              <el-popconfirm
                :title="`确定删除吗？`"
                @onConfirm="deleteSpu(row)"
              >
                <hint-button
                  type="danger"
                  slot="reference"
                  size="mini"
                  icon="el-icon-delete"
                  title="删除spu"
                  >删除</hint-button
                >
              </el-popconfirm>
              <!-- <hint-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                /> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          style="text-align: center"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          :total="total"
          layout="prev, pager, next, jumper,->,sizes,total"
        >
        </el-pagination>
      </div>
      <!-- 添加|修改spu -->
      <SpuForm v-show="scene == 1" @changeScene="changescene" ref="spuForm"
        >1</SpuForm
      >
      <!-- 添加sku -->
      <SkuForm v-show="scene == 2" ref="skuForm" @changeScene="changescene">
        2</SkuForm
      >
    </el-card>
    <!-- 显示sku列表的对话框 -->
    <el-dialog
      :title="skuInfo.spu.spuName"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuInfo.data" v-loading="loading">
        <el-table-column
          label="名称"
          width="width"
          prop="skuName"
        ></el-table-column>
        <el-table-column
          label="价格"
          width="width"
          prop="price"
        ></el-table-column>
        <el-table-column
          label="重量"
          width="width"
          prop="weight"
        ></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row }">
            <img :src="row.skuDefaultImg" alt="" style="width: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  components: {
    SpuForm,
    SkuForm,
  },
  data() {
    return {
      id: {},
      page: 1, //当前几页
      limit: 3, //每页展示数据
      total: 0, // 总数据条数
      records: [], //spu列表数据
      scene: 0, //控制下半部分呢内容的显示与隐藏
      dialogTableVisible: false, // 控制展示数据列表的对话框显示与隐藏
      // 展示列表数据
      skuInfo: {
        spu: {}, // spu信息
        data: [], // 列表信息
      },
      loading: true, //加载效果的显示
    };
  },
  methods: {
    getcategoryid(id) {
      this.id = id;
      this.getSpuList();
    },
    // 获取spu列表
    async getSpuList(pages = 1) {
      this.page = pages;
      const { page, limit } = this;
      let res = await this.$API.spu.reqSpuList(
        page,
        limit,
        this.id.category3Id
      );
      if (res.code == 200) {
        this.records = res.data.records;
        this.total = res.data.total;
      }
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList(this.page);
    },
    //添加spu
    addSpu() {
      this.scene = 1;
      // 触发子组件自定义事件
      // 添加需要携带category3Id，这里将id传给子组件
      this.$refs.spuForm.getMarkList(this.id.category3Id);
    },
    //添加sku
    addSku(row) {
      this.scene = 2;
      // 父组件触发子组件方法传入响应参数
      this.$refs.skuForm.getData(row, this.id);
    },
    // 修改spu
    updateSpu(row) {
      this.scene = 1;
      // 点击时触发子组件的事件
      this.$refs.spuForm.getData(row);
    },
    // 显示所有sku列表
    async showAll(row) {
      this.dialogTableVisible = true;
      this.skuInfo.spu = row;
      // 获取spu列表数据
      let res = await this.$API.sku.reqfindBySpuId(row.id);
      if (res.code == 200) {
        this.skuInfo.data = res.data;
        // 数据回来后关闭loading效果
        this.loading = false;
      }
    },
    // 关闭对话框的回调
    close(done) {
      // 为了避免数据的回显问题，每次关闭前都要清空数据
      this.skuInfo = { spu: {}, data: [] };
      //  打开加载效果
      this.loading = true;
      done()// 关闭对话框
    },
    // 删除spu
    async deleteSpu(row) {
      let res = this.$API.spu.reqDeleteSpu(row.id);
      // 这里返回的是一个promise对象，所以用then
      res.then(() => {
        this.$message("删除成功");
        this.getSpuList(this.records.length > 1 ? this.page : this.page - 1);
      });
    },
    // spuFrom的自定义事件回调
    changescene(data) {
      if (data.scene == "01") {
        if (data.flag == "修改") {
          this.getSpuList(this.page);
        } else {
          this.getSpuList();
        }
        this.scene = 0;
      } else {
        this.scene = 0;
      }
    },
  },
};
</script>

<style scoped>
</style>